package com.ykdl.member.kid.set;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.marketcard.PersonalDataActivity;
import com.ykdl.member.kid.util.PregnancyHelp;
import com.ykdl.member.views.MyWheelView;
import com.ykdl.member.views.MyWheelView.OnWheelViewDataChanger;

public class PredateSetActivity extends BaseScreen{
	private EditText days_et;
	private MyWheelView wheel_time;
	private boolean open = false ;
	private TextView yuejing_time,calc_method;
	private long babybirthday ;
	private String[] predateTime,times_hint;
	private int year,month,day;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTopTitle("预产期设置", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
					finish();
			}
		}, 0, 0,null, R.drawable.title_bg, false);
		setContent(R.layout.activity_predate);
		initView();
		wheel_time = (MyWheelView) findViewById(R.id.accountinfor_wheel_time);
		wheel_time.setEndYesterday(true);
		wheel_time.setVisibility(View.INVISIBLE);
		wheel_time.setWheelView(MyWheelView.CALENDAR);
        wheel_time.findViewById(R.id.my_wheelview_cancel).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				open = false;
				wheel_time.setMyGone();
			}
		});
		wheel_time.setOnWheelViewDataChanger(new OnWheelViewDataChanger() {

			@Override
			public void updataByChanger(String data) {
				if (!open) {
					InputMethodManager mInputMethodManager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
					mInputMethodManager.toggleSoftInput(0, InputMethodManager.RESULT_UNCHANGED_SHOWN);
					
				} else {
					String[] time = data.split(",");
					predateTime=time;
					yuejing_time.setText(time[0]
							+ "年"
							+ time[1]
							+ "月"
							+ time[2]
							+ "日     "
							);
					open = false;
					Date now = new Date();
					now.setYear(Integer.parseInt(time[0])-1900);
					now.setMonth(Integer.parseInt(time[1])-1);
					now.setDate(Integer.parseInt(time[2]));
					babybirthday =now.getTime()/1000;
				}
			}
		});
	}

	private void initView() {
		// TODO Auto-generated method stub
		Date date=new Date();
		SimpleDateFormat format=new SimpleDateFormat("yyyy,MM,dd");
		String time_hint=format.format(date); 
		times_hint = time_hint.split(",");  //设置末次月经时间的默认值
		yuejing_time=(TextView) findViewById(R.id.yuejing_time);
//		yuejing_time.setHint(times_hint[0]+ "年"+ times_hint[1]+ "月"+ times_hint[2]+ "日  ");
		days_et=(EditText) findViewById(R.id.days_et);
		calc_method=(TextView) findViewById(R.id.calc_method);
		calc_method.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);// 下划线
		calc_method.setOnClickListener(new OnClickListener() {//如何计算预产期
		
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(mContext, SetDeclare.class);
				intent.putExtra("COMEFROM", "PredateSetActivity");
				startActivity(intent);
			}
		  });
	    
		findViewById(R.id.time_bg).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				wheel_time.setVisibility(View.VISIBLE);
				open = true;
				((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
				.hideSoftInputFromWindow(PredateSetActivity.this.getCurrentFocus()
						.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  
				wheel_time.setInVisible(true);
				wheel_time.setMyVisible();
			}
		});
		findViewById(R.id.submit_bt).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				save();
			}
		});
	}
	
	private void save(){
		String strDate = yuejing_time.getText().toString();
		if (TextUtils.isEmpty(strDate)) {
//			predateTime=times_hint;
			Toast.makeText(this, "请输入末次月经时间", 1).show();
			return;
		}
		String days = days_et.getText().toString();
		if (TextUtils.isEmpty(days)) {
			days="29";
		}
		if(Integer.valueOf(days)<20 || Integer.valueOf(days)>45){
			Toast.makeText(this, "请输入合法的周期天数", 1).show();
			return;
		}
		
		PregnancyHelp ph=new PregnancyHelp();
		try {
			String predate=ph.getPredate(predateTime[0]+"-"+predateTime[1]+"-"+predateTime[2],Integer.valueOf(days)-28);
			Intent intent=new Intent();
			intent.putExtra("PREDATE", predate);
			setResult(100, intent);
			finish();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
